package com.kailong.dao.admin;

import com.kailong.dao.BaseDao;
import com.kailong.pojo.Admin;
import com.kailong.pojo.Student;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ManageAdminDao {
    public static int checkAdminByano(String ano,String apwd){
        int count = 0;
        Student student = null;
        //声明结果集
        ResultSet rs = null;
        //获取连接对象
        Connection conn = BaseDao.getconn();

        PreparedStatement ps = null;
        try{
            String sql = "select count(*) from admin where ano = ? and apassword = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,ano);
            ps.setString(2,apwd);
            rs = ps.executeQuery();
            while (rs.next()){
                count = rs.getInt(1);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            BaseDao.closeall(rs,ps,conn);
        }


        return  count;
    }
    public static Admin selectAdmin(String ano, String apwd){
        Admin admin = null;
        ResultSet rs = null;
        Connection conn = BaseDao.getconn();
        PreparedStatement ps = null;
        try{
            String sql = "select * from admin where ano = ? and apassword = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,ano);
            ps.setString(2,apwd);
            rs = ps.executeQuery();
            while(rs.next()){
                admin = new Admin(
                        rs.getString("ano"),
                        rs.getString("aname"),
                        rs.getString("apassword"),
                        rs.getString("aphonenumber")
                );
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return  admin;
    }
    public  static int updatePwd(String ano,String pwdnew){
        int count =0;
        Connection conn = BaseDao.getconn();
        PreparedStatement ps = null;
        try {
            String sql = "update admin set apassword = ? where ano = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,pwdnew);
            ps.setString(2,ano);
            count = ps.executeUpdate();
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            BaseDao.closeall(null,ps,conn);
        }
        return count;
    }
}
